package cn.edu.cug.cs.gtl.apps.ui;


import cn.edu.cug.cs.gtl.utils.impl.UserManagerImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;
import java.util.ResourceBundle;

public class RegisterDialog extends JDialog {
    static ResourceBundle resourceBundle = ResourceBundle.getBundle("app_resource", Locale.getDefault());

    UserManagerImpl userManager = new UserManagerImpl();

    private JTextField usernameField;
    private JPasswordField passwordField;
    private JPasswordField confirmPasswordField;
    private JTextField phoneField;
    private JButton registerButton;
    private JButton cancelButton;
    private boolean isRegistered = false;

    public RegisterDialog(JFrame owner) {
        super(owner, resourceBundle.getString("login.register.title"), true);
        initComponents();
        pack();
        setLocationRelativeTo(owner);
    }

    private void initComponents() {
        setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
        setSize(300, 250);

        usernameField = new JTextField(20);
        passwordField = new JPasswordField(20);
        confirmPasswordField = new JPasswordField(20);
        phoneField = new JTextField(20);

        JPanel buttonPane = new JPanel();
        registerButton = new JButton(resourceBundle.getString("login.register.register"));
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onRegister();
            }
        });
        cancelButton = new JButton(resourceBundle.getString("login.register.cancel"));
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        buttonPane.add(registerButton);
        buttonPane.add(cancelButton);

        add(new JLabel(resourceBundle.getString("login.register.username")));
        add(usernameField);
        add(new JLabel(resourceBundle.getString("login.register.password")));
        add(passwordField);
        add(new JLabel(resourceBundle.getString("login.register.password2")));
        add(confirmPasswordField);
        add(new JLabel(resourceBundle.getString("login.register.phone")));
        add(phoneField);
        add(buttonPane);
    }

    private void onRegister() {
        // 这里添加注册逻辑
        String username = usernameField.getText();
        String password = new String(passwordField.getPassword());
        String confirmPassword = new String(confirmPasswordField.getPassword());
        String phone = phoneField.getText();

        // 检查输入是否为空
        if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()) {
            JOptionPane.showMessageDialog(this, resourceBundle.getString("login.empty"), "", JOptionPane.ERROR_MESSAGE);
            return;
        }

        // 简单的验证，确保两次密码输入一致
        if (!password.equals(confirmPassword)) {
            JOptionPane.showMessageDialog(this, resourceBundle.getString("login.register.password.not.match"), "", JOptionPane.ERROR_MESSAGE);
            return;
        }

        boolean result = userManager.register(username, password, phone);
        isRegistered = result;

        // 如果注册成功
        if (isRegistered) {
            // 创建一个带自定义按钮的 JOptionPane 对话框
            Object[] options = {resourceBundle.getString("login.register.gologin")};
            int choice = JOptionPane.showOptionDialog(
                    this,
                    resourceBundle.getString("login.register.success"),
                    "",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.INFORMATION_MESSAGE,
                    null,
                    options,
                    options[0]
            );

            // 检查用户是否点击了"去登录"
            if (choice == 0) {
                // 关闭注册窗口
                dispose();
                // 打开登录窗口
                new LoginDialog(null).setVisible(true);
            }
        } else {
            JOptionPane.showMessageDialog(this, resourceBundle.getString("login.register.failed"), "", JOptionPane.ERROR_MESSAGE);
        }
    }

}
